package com.heartware.sh.blackhole.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.adapter.MyAdapter;
import com.heartware.sh.blackhole.entity.Entity;
import com.heartware.sh.blackhole.entity.NameBean;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;
import com.heartware.sh.blackhole.view.IOneView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/25 0025.
 */
public class OneActivity extends BaseActivity implements IOneView{

    private static final String TAG = OneActivity.class.getSimpleName();

    private RecyclerView rv;

    private MyAdapter adapter;


    List<String> stringList = new ArrayList<>();

    private TextView tvStickyHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getToolBarX().setTitle(R.string.app_name).setNavigationIcon(R.mipmap.ic_launcher);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_one;
    }

    @Override
    public void initView() {
        super.initView();

        initData();


        rv = (RecyclerView) findViewById(R.id.rv);
        tvStickyHeaderView = (TextView) findViewById(R.id.tv_sticky_header_view);
        tvStickyHeaderView.setBackgroundColor(Color.BLACK);

        /** 设置item动画*/
        rv.setItemAnimator(new DefaultItemAnimator());
//        /** 设置item Divider  ListView*/
//        rv.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));

//        rv.addItemDecoration(new SectionDecoration(list, this, new SectionDecoration.DecorationCallback() {
//            @Override
//            public String getGroupId(int position) {
//                if(list.get(position).getName()!=null) {
//                    return list.get(position).getName();
//                }
//                return "-1";
//            }
//
//            @Override
//            public String getGroupFirstLine(int position) {
//                if(list.get(position).getName()!=null) {
//                    return list.get(position).getName();
//                }
//                return "";
//            }
//        }));

        /** 设置Recyclerview显示样式,   类似Listview*/
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);

        /** 设置RecyclerView显示样式,  类似于GridView  横向滑动GridView*/
//        RecyclerView.LayoutManager manager = new StaggeredGridLayoutManager(10, StaggeredGridLayoutManager.HORIZONTAL);

        /** 设置item Divider  GridView*/
//        rv.addItemDecoration(new DividerGridItemDecoration(mContext));

        /** 设置RecyclerView显示样式, 类似于GridView*/
//        RecyclerView.LayoutManager manager = new GridLayoutManager(this, 3);

        rv.setLayoutManager(manager);

        adapter = new MyAdapter(stringList);

        adapter.setItemClickListener(new MyAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(mContext, stringList.get(position), Toast.LENGTH_SHORT).show();

            }
        });

        adapter.setItemLongClickListener(new MyAdapter.MyItemLongClickListener() {
            @Override
            public void onItemLongClick(View v, int position) {
                Toast.makeText(mContext, "长按:"+stringList.get(position), Toast.LENGTH_SHORT).show();
//                adapter.notifyItemRemoved(position);
                stringList.remove(position);
                adapter.notifyDataSetChanged();
            }
        });

        rv.setAdapter(adapter);

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                View stickyInfoView = recyclerView.findChildViewUnder(
                        tvStickyHeaderView.getMeasuredWidth() / 2, 5);

                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
                    tvStickyHeaderView.setText(String.valueOf(stickyInfoView.getContentDescription()));
                }

                View transInfoView = recyclerView.findChildViewUnder(
                        tvStickyHeaderView.getMeasuredWidth() / 2, tvStickyHeaderView.getMeasuredHeight() + 1);

                if (transInfoView != null && transInfoView.getTag() != null) {

                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - tvStickyHeaderView.getMeasuredHeight();

                    if (transViewStatus == adapter.HAS_STICKY_VIEW) {
                        if (transInfoView.getTop() > 0) {
                            tvStickyHeaderView.setTranslationY(dealtY);
                        } else {
                            tvStickyHeaderView.setTranslationY(0);
                        }
                    } else if (transViewStatus == adapter.NONE_STICKY_VIEW) {
                        tvStickyHeaderView.setTranslationY(0);
                    }
                }

            }
        });

    }

    @Override
    public void setListener() {
        super.setListener();
    }

    @Override
    public void showResult(List<Entity> value) {

        Log.i(TAG, new Gson().toJson(value));

    }

    @Override
    public void showResult(String value) {


    }



    public void initData(){

        NameBean name = null;
        for(int i=0; i<40; i++){

            if(i < 10){
              stringList.add("测试数据"+0);
            }else if(i < 20){
              stringList.add("测试数据"+1);
            }else if(i < 30){
              stringList.add("测试数据"+2);
            }else if(i < 40){
              stringList.add("测试数据"+3);
            }



        }

    }

}
